﻿using VIA.Integration;
using VIA.Integration.Alarms;
using VIA.Integration.LeadShine;

namespace VIA.SiMoJi.Domain.Electronics
{
    public class Nozzle : CylinderVacuo,ISuportClearAlarm
    {
        private bool _isSensorOnError;
        private bool _isSensorOffError;

        [Alarm(true, AlarmLevel.Error, Description = "吸真空异常")]
        public bool IsSensorOnError { get => _isSensorOnError; set => this.NotifyAlarm(ref _isSensorOnError, value); }

        [Alarm(true, AlarmLevel.Error, Description = "破真空异常")]
        public bool IsSensorOffError { get => _isSensorOffError; set => this.NotifyAlarm(ref _isSensorOffError, value); }
        public IoPoint Sensor { get; set; }
        public Nozzle(IoPoint suckIo, IoPoint brokenIo, IoPoint keepIo) : base(suckIo, brokenIo, keepIo)
        {
            this.InitalizeAlarms();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="suckIo">吸真空</param>
        /// <param name="brokenIo">吹真空</param>
        /// <param name="keepIo">气缸伸出</param>
        /// <param name="sensor">真空表</param>
        public Nozzle(IoPoint suckIo, IoPoint brokenIo, IoPoint keepIo, IoPoint sensor) : base(suckIo, brokenIo, keepIo)
        {
            Sensor = sensor;
        }

        public async Task<bool> SuckAndCheckAsync(CancellationToken cancellationToken = default, int timeout = 1500, int checkDelay = 200, int interval = 25)
        {
            SuckAsync();
            if (checkDelay > 0)
                await Task.Delay(checkDelay);
            if (!await Judger.Sure(() => IsOn(), cancellationToken, timeout, interval))
            {
                IsSensorOnError = true;
                return false;
            }
            return true;
        }

        public async Task<bool> BrokenAndCheckAsync(CancellationToken cancellationToken = default, int timeout = 1500, int checkDelay = 200, int interval = 25)
        {
            BrokenAsync();
            if (checkDelay > 0)
                await Task.Delay(checkDelay);
            if (!await Judger.Sure(() => !IsOn(), cancellationToken, timeout, interval))
            {
                IsSensorOffError = true;
                return false;
            }
            return true;
        }

        public async Task<bool> StopAndCheckAsync(CancellationToken cancellationToken = default, int timeout = 1500, int checkDelay = 200, int interval = 25)
        {
            StopAsync();
            if (checkDelay > 0)
                await Task.Delay(checkDelay);
            if (!await Judger.Sure(() => !IsOn(), cancellationToken, timeout, interval))
            {
                IsSensorOffError = true;
                return false;
            }
            return true;
        }

        /// <summary>
        /// 真空表是否有信号
        /// </summary>
        /// <returns></returns>
        public bool IsOn()
        {
            if (Sensor != null) return Sensor.Value;
            else
                return false;
        }

        public void ClearAlarm()
        {
            IsSensorOnError = false;
            IsSensorOffError = false;
        }
    }
}
